package cn.itcast.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.dao.MenuDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import cn.itcast.vo.MenuListVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Transactional(readOnly = true)
    @Override
    public PageResult findByPage(Integer currentPage, Integer pageSize) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //根据条件分页查询检查项信息
        Page<Menu> page = menuDao.findParentMenu();
        //构建分页数据返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Boolean save(List<Integer> permissionIds, Menu menu) {
        Integer integer = menuDao.save(menu);
        menu = menuDao.findByName(menu.getName());
        int count = 1;
        if (CollUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                integer += menuDao.saveMenuAndPermission(menu.getId(), permissionId);
            }
            count += permissionIds.size();
        }
        return integer == count;
    }

    @Override
    public Boolean delete(Integer menuId) {
        Integer count = menuDao.findRoleIdByMenuId(menuId);
        if (count > 0) {
            return false;
        }
        menuDao.deleteMenuAndPermission(menuId);
        menuDao.deleteByParentMenuId(menuId);

        menuDao.deleteMenuAndRole(menuId);
        return menuDao.delete(menuId) > 0;
    }

    @Override
    public Boolean edit(List<Integer> permissionIds, Menu menu) {
        menuDao.deleteMenuAndPermission(menu.getId());

        if (CollUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                menuDao.saveMenuAndPermission(menu.getId(), permissionId);
            }
        }
        return menuDao.edit(menu) > 0;
    }

    @Override
    @Transactional(readOnly = true)
    public Menu findById(Integer id) {
        return menuDao.findByid(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Integer> findPermissionIdsByMenuId(Integer menuId) {
        return menuDao.findPermissionIdsByMenuId(menuId);
    }

    @Transactional(readOnly = true)
    @Override
    public List<MenuListVo> findMenuByUserId(Integer userId) {
        List<MenuListVo> menuListVos = menuDao.findMenuByUserId(userId);
        List<String> names = menuDao.findMenuNameByUserId(userId);

        for (int i = 0; i < menuListVos.size(); i++) {
            for (int j = 0; j < menuListVos.get(i).getChildren().size(); j++) {
                if (!CollectionUtil.contains(names, menuListVos.get(i).getChildren().get(j).getTitle())) {
                    menuListVos.get(i).getChildren().remove(j);
                    j--;
                }
            }
        }
        return menuListVos;
    }
}
